﻿#include "lua_obj.h"

static int lua_lv_label_set_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    char *text = (char *)luaL_checkstring(L, 2);
    lv_label_set_text(a, text);

    return 0;
}

static int lua_lv_label_set_text_fmt(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_set_text_static(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_set_long_mode(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_set_recolor(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_set_text_sel_start(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint32_t index = luaL_checkinteger(L, 2);
    lv_label_set_text_sel_start(a, index);

    return 0;
}

static int lua_lv_label_set_text_sel_end(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint32_t index = luaL_checkinteger(L, 2);
    lv_label_set_text_sel_end(a, index);

    return 0;
}

static int lua_lv_label_get_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_get_long_mode(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_get_recolor(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_get_letter_pos(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint32_t char_id = luaL_checkinteger(L, 2);

    return 0;
}

static int lua_lv_label_get_letter_on(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    lv_point_t *pos_in = luaL_checkinteger(L, 2);

    return 1;
}

static int lua_lv_label_is_char_under_pos(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);

    return 0;
}

static int lua_lv_label_get_text_selection_start(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint16_t res = lv_label_get_text_selection_start(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_label_get_text_selection_end(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint16_t res = lv_label_get_text_selection_end(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_label_ins_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint32_t pos = luaL_checkinteger(L, 2);

    return 0;
}

static int lua_lv_label_cut_text(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    uint32_t pos = luaL_checkinteger(L, 2);
    uint32_t cnt = luaL_checkinteger(L, 3);
    lv_label_cut_text(a, pos, cnt);

    return 0;
}

static int lua_auto_gc(lua_State *L)
{
    lv_obj_t *lv_obj = *(lv_obj_t **)luaL_checkudata(L, 1, META_LABEL);
    luaL_argcheck(L, lv_obj != NULL, 1, "invalid user data");
    if (lv_obj)
    {
        lv_obj_del(lv_obj);
    }
    return 0;
}
static const struct luaL_Reg arraylib_m[] = {
    {"set_text", lua_lv_label_set_text},
    {"set_text_fmt", lua_lv_label_set_text_fmt},
    {"set_text_static", lua_lv_label_set_text_static},
    {"set_long_mode", lua_lv_label_set_long_mode},
    {"set_recolor", lua_lv_label_set_recolor},
    {"set_text_sel_start", lua_lv_label_set_text_sel_start},
    {"set_text_sel_end", lua_lv_label_set_text_sel_end},
    {"get_text", lua_lv_label_get_text},
    {"get_long_mode", lua_lv_label_get_long_mode},
    {"get_recolor", lua_lv_label_get_recolor},
    {"get_letter_pos", lua_lv_label_get_letter_pos},
    {"get_letter_on", lua_lv_label_get_letter_on},
    {"is_char_under_pos", lua_lv_label_is_char_under_pos},
    {"get_text_selection_start", lua_lv_label_get_text_selection_start},
    {"get_text_selection_end", lua_lv_label_get_text_selection_end},
    {"ins_text", lua_lv_label_ins_text},
    {"cut_text", lua_lv_label_cut_text},
    LUA_LV_OBJ_RLT,
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(arraylib_m) / sizeof(struct luaL_Reg);
    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\n", arraylib_m[i].name);
    }
    return 1;
}

static const struct luaL_Reg arraylib_f[] = {
    {"help", help},
    {NULL, NULL}};

LUALIB_API int lua_lv_label_funinit(lua_State *L)
{
    luaL_newlib(L, arraylib_f);
    luaL_newmetatable(L, META_LABEL); /* create metatable for file handles */
    lua_pushvalue(L, -1);             /* push metatable */
    lua_setfield(L, -2, "__index");   /* metatable.__index = metatable */
    luaL_setfuncs(L, arraylib_m, 0);  /* add file methods to new metatable */
    lua_pop(L, 1);                    /* pop new metatable */

    return 1;
}
